package clang.control;

import clang.data.Expression;

public class ForLoop implements CodeElement {

	private CodeBlock block = null;
	private Expression init = null, cond = null, inc = null;
	
	public ForLoop(CodeBlock block, Expression init, Expression cond,
			Expression inc) {
		this.block = block;
		this.init = init;
		this.cond = cond;
		this.inc = inc;
	}

	@Override
	public void execute() throws Exception {
		if(block == null) throw new Exception("No code block in for-loop");
		
		if(init != null) init.execute();
		
		while((cond != null) ? cond.calculate().getBool() : true)
		{
			block.execute();
			if(inc != null) inc.calculate();
		}
	}

}
